refundApply<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单明细id" prop="orderDetailId">
        <el-input
          v-model="queryParams.orderDetailId"
          placeholder="请输入订单明细id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单id" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流水支付单号" prop="payOrderNo">
        <el-input
          v-model="queryParams.payOrderNo"
          placeholder="请输入流水支付单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流水退款单号" prop="refundOrderNo">
        <el-input
          v-model="queryParams.refundOrderNo"
          placeholder="请输入流水退款单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单所属用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入订单所属用户id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款金额" prop="refundAmount">
        <el-input
          v-model="queryParams.refundAmount"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请退款原因" prop="refundReason">
        <el-input
          v-model="queryParams.refundReason"
          placeholder="请输入申请退款原因"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款状态描述" prop="message">
        <el-input
          v-model="queryParams.message"
          placeholder="请输入退款状态描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批人id" prop="approver">
        <el-input
          v-model="queryParams.approver"
          placeholder="请输入审批人id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批意见" prop="approveOpinion">
        <el-input
          v-model="queryParams.approveOpinion"
          placeholder="请输入审批意见"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款失败原因" prop="failedReason">
        <el-input
          v-model="queryParams.failedReason"
          placeholder="请输入退款失败原因"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款问题说明" prop="questionDesc">
        <el-input
          v-model="queryParams.questionDesc"
          placeholder="请输入退款问题说明"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款渠道" prop="refundChannel">
        <el-input
          v-model="queryParams.refundChannel"
          placeholder="请输入退款渠道"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批时间" prop="approveTime">
        <el-date-picker clearable
          v-model="queryParams.approveTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择审批时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="退款完成时间" prop="finishTime">
        <el-date-picker clearable
          v-model="queryParams.finishTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择退款完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建人" prop="creater">
        <el-input
          v-model="queryParams.creater"
          placeholder="请输入创建人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新人" prop="updater">
        <el-input
          v-model="queryParams.updater"
          placeholder="请输入更新人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['order:refundApply:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:refundApply:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:refundApply:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['order:refundApply:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="refundApplyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="退款id" align="center" prop="id" />
      <el-table-column label="订单明细id" align="center" prop="orderDetailId" />
      <el-table-column label="订单id" align="center" prop="orderId" />
      <el-table-column label="流水支付单号" align="center" prop="payOrderNo" />
      <el-table-column label="流水退款单号" align="center" prop="refundOrderNo" />
      <el-table-column label="订单所属用户id" align="center" prop="userId" />
      <el-table-column label="退款金额" align="center" prop="refundAmount" />
      <el-table-column label="退款状态，1：待审批，2：取消退款，3：同意退款，4：拒绝退款，5：退款成功，6：退款失败" align="center" prop="status" />
      <el-table-column label="申请退款原因" align="center" prop="refundReason" />
      <el-table-column label="退款状态描述" align="center" prop="message" />
      <el-table-column label="审批人id" align="center" prop="approver" />
      <el-table-column label="审批意见" align="center" prop="approveOpinion" />
      <el-table-column label="审批备注" align="center" prop="remark" />
      <el-table-column label="退款失败原因" align="center" prop="failedReason" />
      <el-table-column label="退款问题说明" align="center" prop="questionDesc" />
      <el-table-column label="退款渠道" align="center" prop="refundChannel" />
      <el-table-column label="审批时间" align="center" prop="approveTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.approveTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款完成时间" align="center" prop="finishTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="creater" />
      <el-table-column label="更新人" align="center" prop="updater" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['order:refundApply:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['order:refundApply:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退款申请对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="refundApplyRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单明细id" prop="orderDetailId">
          <el-input v-model="form.orderDetailId" placeholder="请输入订单明细id" />
        </el-form-item>
        <el-form-item label="订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单id" />
        </el-form-item>
        <el-form-item label="流水支付单号" prop="payOrderNo">
          <el-input v-model="form.payOrderNo" placeholder="请输入流水支付单号" />
        </el-form-item>
        <el-form-item label="流水退款单号" prop="refundOrderNo">
          <el-input v-model="form.refundOrderNo" placeholder="请输入流水退款单号" />
        </el-form-item>
        <el-form-item label="订单所属用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入订单所属用户id" />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundAmount">
          <el-input v-model="form.refundAmount" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="申请退款原因" prop="refundReason">
          <el-input v-model="form.refundReason" placeholder="请输入申请退款原因" />
        </el-form-item>
        <el-form-item label="退款状态描述" prop="message">
          <el-input v-model="form.message" placeholder="请输入退款状态描述" />
        </el-form-item>
        <el-form-item label="审批人id" prop="approver">
          <el-input v-model="form.approver" placeholder="请输入审批人id" />
        </el-form-item>
        <el-form-item label="审批意见" prop="approveOpinion">
          <el-input v-model="form.approveOpinion" placeholder="请输入审批意见" />
        </el-form-item>
        <el-form-item label="审批备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入审批备注" />
        </el-form-item>
        <el-form-item label="退款失败原因" prop="failedReason">
          <el-input v-model="form.failedReason" placeholder="请输入退款失败原因" />
        </el-form-item>
        <el-form-item label="退款问题说明" prop="questionDesc">
          <el-input v-model="form.questionDesc" placeholder="请输入退款问题说明" />
        </el-form-item>
        <el-form-item label="退款渠道" prop="refundChannel">
          <el-input v-model="form.refundChannel" placeholder="请输入退款渠道" />
        </el-form-item>
        <el-form-item label="审批时间" prop="approveTime">
          <el-date-picker clearable
            v-model="form.approveTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择审批时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退款完成时间" prop="finishTime">
          <el-date-picker clearable
            v-model="form.finishTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择退款完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人" prop="creater">
          <el-input v-model="form.creater" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="更新人" prop="updater">
          <el-input v-model="form.updater" placeholder="请输入更新人" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RefundApply">
import { listRefundApply, getRefundApply, delRefundApply, addRefundApply, updateRefundApply } from "@/api/order/refundApply";

const { proxy } = getCurrentInstance();

const refundApplyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderDetailId: null,
    orderId: null,
    payOrderNo: null,
    refundOrderNo: null,
    userId: null,
    refundAmount: null,
    status: null,
    refundReason: null,
    message: null,
    approver: null,
    approveOpinion: null,
    failedReason: null,
    questionDesc: null,
    refundChannel: null,
    approveTime: null,
    finishTime: null,
    creater: null,
    updater: null
  },
  rules: {
    orderDetailId: [
      { required: true, message: "订单明细id不能为空", trigger: "blur" }
    ],
    orderId: [
      { required: true, message: "订单id不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "订单所属用户id不能为空", trigger: "blur" }
    ],
    refundAmount: [
      { required: true, message: "退款金额不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "退款状态，1：待审批，2：取消退款，3：同意退款，4：拒绝退款，5：退款成功，6：退款失败不能为空", trigger: "change" }
    ],
    refundReason: [
      { required: true, message: "申请退款原因不能为空", trigger: "blur" }
    ],
    message: [
      { required: true, message: "退款状态描述不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "创建退款申请时间不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ],
    creater: [
      { required: true, message: "创建人不能为空", trigger: "blur" }
    ],
    updater: [
      { required: true, message: "更新人不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询退款申请列表 */
function getList() {
  loading.value = true;
  listRefundApply(queryParams.value).then(response => {
    refundApplyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    orderDetailId: null,
    orderId: null,
    payOrderNo: null,
    refundOrderNo: null,
    userId: null,
    refundAmount: null,
    status: null,
    refundReason: null,
    message: null,
    approver: null,
    approveOpinion: null,
    remark: null,
    failedReason: null,
    questionDesc: null,
    refundChannel: null,
    createTime: null,
    approveTime: null,
    finishTime: null,
    updateTime: null,
    creater: null,
    updater: null
  };
  proxy.resetForm("refundApplyRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加退款申请";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getRefundApply(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改退款申请";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["refundApplyRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateRefundApply(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRefundApply(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除退款申请编号为"' + _ids + '"的数据项？').then(function() {
    return delRefundApply(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('order/refundApply/export', {
    ...queryParams.value
  }, `refundApply_${new Date().getTime()}.xlsx`)
}

getList();
</script>

